package com.example.market.service;


import com.example.market.exception.ExceptionEnum;
import com.example.market.exception.ServiceException;
import com.example.market.mapper.ProductMapper;
import com.example.market.pojo.Product;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

//import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

@Service
public class ProductService {

//    @Autowired
    private ProductMapper productMapper;

    @Autowired
    public void setProductMapper(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    public List<Product> getProductByCategoryId(Integer categoryId) {
        List<Product> list;
        Example example = new Example(Product.class);
        example.orderBy("productSales").desc();
        example.createCriteria().andEqualTo("categoryId", categoryId);
        try {
            list = productMapper.selectByExampleAndRowBounds(example,new RowBounds(0,4));
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new ServiceException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return list;
    }

    public Product getProductById(String productId) {
        Product product;
        try {
            product = productMapper.selectByPrimaryKey(productId);
            if (product == null) {
                throw new ServiceException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return product;
    }

    public List<Product> getHotProduct() {
        Example example = new Example(Product.class);
        example.orderBy("productSales").desc();
        List<Product> list;
        try {
            list = productMapper.selectByExampleAndRowBounds(example,new RowBounds(0,8));
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new ServiceException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return list;
    }
    public List<Product> getProductByPage(String currentPage, String pageSize, String categoryId) {
        List<Product> list;
        Product product = new Product();
        if (!categoryId.equals("0")) {
            product.setCategoryId(Integer.parseInt(categoryId));
        }
        list = productMapper.selectByRowBounds(product, new RowBounds((Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize), Integer.parseInt(pageSize)));
//        System.out.println(Integer.parseInt(currentPage) - 1);
//        System.out.println(Integer.parseInt(pageSize));
//        System.out.println(list);
        return list;
    }

    public Integer getTotal(String categoryId) {
        Product product = new Product();
        if (!categoryId.equals("0")) { product.setCategoryId(Integer.parseInt(categoryId));}
        return productMapper.selectCount(product);
    }

    public List<Product> getProductBySearch(String search, String currentPage, String pageSize) {
        String searchKey = "%" + search + "%";
        int begin = Integer.parseInt(currentPage) - 1;
        int end = begin + Integer.parseInt(pageSize);
        return productMapper.search(searchKey, begin, end);
    }

    public int searchCount(String search) {
        String searchKey = "%" + search + "%";
        return productMapper.searchCount(searchKey);
    }

}
